//获取应用实例
const app = getApp()
Page({
  data: {
    // 区域
    regionArray: [],
    regionIndex: [0, 0, 0],
    //  面积
    area: [],
    index1: 0,
    //  价格
    price: [],
    index2: 0,
    //  类型
    type: [],
    index3: 0,
    //  装修
    trim: [],
    index4: 0,
    // 房源列表
    project: [],
    // 搜索条件
    // 区id
    country: '',
    // 街id
    street: '',
    mjid: '',
    jgid: '',
    fyid: '',
    zxid: '',
    key: '',
    // 选中的区域
    acRegion: '',
    // 选中的面积
    acArea: '',
    // 选中的价格
    acPrice: '',
    // 选中的类型
    acTypes: '',
    // 选中的装修
    acDecoration: '',
    // 当前page
    p: 1,
  },
  onHide() {
    this.setData({
      project: [],
      key: '',
      p: '1',
    })
    app.globalData.key = ''
  },
  onLoad: function (options) {
    let that = this
    let menu = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: function (res) {
        let cacl = 750 / res.windowWidth
        that.setData({
          cacl: cacl,
          menuTop: menu.top,
          menuH: menu.height,
          menuR: res.windowWidth - menu.left,
        })
      },
    })
    //创建节点选择器
    var query = wx.createSelectorQuery()
    query.select('#custom').boundingClientRect()
    query.exec(function (res) {
      //res就是 所有标签为mjltest的元素的信息 的数组
      that.setData({
        customH: res[0].height,
      })
    })

    this._getCity()
    this._getMjlist()
    this._getPrice()
    this._getType()
    this._getTrim()
  },
  onShow() {
    const key = app.globalData.key
    this.setData({ key })
    this._getList()
  },
  // 获取城市
  async _getCity() {
    // 获取首页数据
    let res = await wx.requestAsync({
      url: 'index/cslist',
    })

    const citys = res.res.city
    // 城市
    let regionArray = [[{ title: '不限', id: '-1' }, ...citys], [], []]
    this.setData({
      regionArray,
      citys,
    })
    // 默认城市id
    const cid = citys[0].id
    this._getDzlist({ cid })
  },
  // 获取区
  async _getDzlist({ cid }) {
    const { regionArray } = this.data
    // 获取首页数据
    let res = await wx.requestAsync({
      url: 'index/dzlist',
      data: {
        id: cid,
      },
    })
    console.log(res.res.zone)
    const diqu = res.res.zone
    // 区
    if (regionArray) {
      regionArray[1] = [{ title: '不限', id: '-1' }, ...diqu]
    }
    this.setData({
      regionArray,
      diqu,
    })
    if (diqu) {
      let did = diqu[0].id
      this._getJiedao({ did })
    }
  },
  // 获取街道
  async _getJiedao({ did }) {
    const { regionArray } = this.data
    let res = await wx.requestAsync({
      url: 'index/street',
      data: { id: did },
    })
    const jiedao = res.res.street
    if (regionArray) {
      regionArray[2] = [{ title: '不限', id: '-1' }, ...jiedao]
      this.setData({
        regionArray,
      })
    }
  },
  // 获取面积
  async _getMjlist() {
    let res = await wx.requestAsync({
      url: 'index/mjlist',
    })
    // console.log("res", res.res.mjlist);
    const mjlist = res.res.mjlist
    mjlist.map((item) => (item.title = item.start + '-' + item.end))
    let area = [{ id: '-1', title: '不限' }, ...mjlist]
    this.setData({ area })
  },
  // 获取价格
  async _getPrice() {
    let res = await wx.requestAsync({
      url: 'index/jglist',
    })
    const jglist = res.res.jglist
    jglist.map((item) => (item.title = item.start + '-' + item.end))
    let price = [{ id: '-1', title: '不限' }, ...jglist]
    this.setData({ price })
  },
  // 获取类型
  async _getType() {
    let res = await wx.requestAsync({
      url: 'index/fylist',
    })
    const fylist = res.res.fylist
    let type = [{ id: '-1', title: '不限' }, ...fylist]
    this.setData({ type })
  },
  //装修
  async _getTrim() {
    let res = await wx.requestAsync({
      url: 'index/zxlist',
    })
    const zxlist = res.res.zxlist
    let trim = [{ id: '-1', title: '不限' }, ...zxlist]
    this.setData({ trim })
  },
  // 获取房源
  async _getList() {
    const {
      country,
      street,
      mjid,
      jgid,
      fyid,
      zxid,
      key,
      city,
      p,
      project,
    } = this.data

    let res = await wx.requestAsync({
      url: 'index/index',
      data: {
        city: city == '-1' ? '' : city,
        country: country == '-1' ? '' : country,
        street: street == '-1' ? '' : street,
        mjid,
        jgid,
        fyid,
        zxid,
        key,
        p,
      },
    })
    if (res.res) {
      console.log(res.res)

      let array = res.res.project
      array.map((item1) => {
        let arr = item1.mjlist.map((item2) => {
          return (item2 = item2 + 'm²')
        })
        item1.mjlist = arr
      })
      console.log(project)
      let projects = [...project, ...array]
      this.setData({
        project: projects,
      })
    } else {
      let project = []
      this.setData({
        project,
      })
    }
  },
  // 区域选择
  bindRegionPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let value = e.detail.value
    let { regionArray } = this.data
    const city = regionArray[0][value[0]].id
    const country = regionArray[1][value[1]].id
    const street = regionArray[2][value[2]].id
    // const acRegion1 = regionArray[1][value[1]].title
    const acRegion = regionArray[2][value[2]].title
    this.setData({
      city,
      country,
      street,
      regionIndex: value,
      acRegion,
      // acRegion:acRegion2
      project: [],
      p: 1,
    })
    if (city || country || street) {
      this._getList()
    }
  },
  // 区域滚动选择
  bindRegionPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value)
    const index = e.detail.value
    const { diqu, city, citys, regionArray } = this.data
    if (e.detail.column == 0) {
      const cid = regionArray[0][index].id
      this._getDzlist({ cid })
    } else if (e.detail.column == 1 && city != -1) {
      const did = diqu[index].id
      this._getJiedao({ did })
    }
  },
  // 获取面积
  // 面积选择
  bindAreaChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let value = e.detail.value
    const { area } = this.data
    const mjid = area[value].id
    console.log(area[value])

    const acArea = area[value].title
    if (mjid != -1) {
      this.setData({
        index1: value,
        mjid,
        acArea,
        project: [],
        p: 1,
      })
      this._getList()
    } else {
      this.setData({
        mjid: null,
        acArea: '面积',
        project: [],
        p: 1,
      })
      this._getList()
    }
  },
  // 价格选择
  bindPriceChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let value = e.detail.value
    const { price } = this.data
    const jgid = price[value].id
    const acPrice = price[value].title
    if (jgid != -1) {
      this.setData({
        index2: value,
        jgid,
        acPrice,
        project: [],
        p: 1,
      })
      this._getList()
    } else {
      this.setData({
        jgid: null,
        acPrice: '价格',
        project: [],
        p: 1,
      })
      this._getList()
    }
  },
  // 类型选择
  bindTypeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let value = e.detail.value
    const { type } = this.data
    const fyid = type[value].id
    const acTypes = type[value].title
    if (fyid != -1) {
      this.setData({
        index3: value,
        fyid,
        acTypes,
        project: [],
        p: 1,
      })
      this._getList()
    } else {
      this.setData({
        fyid: null,
        acTypes: '类型',
        project: [],
        p: 1,
      })
      this._getList()
    }
  },
  // 装修选择
  bindTrimChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let value = e.detail.value
    const { trim } = this.data
    const zxid = trim[value].id
    const acDecoration = trim[value].title
    if (zxid != -1) {
      this.setData({
        index4: value,
        zxid,
        acDecoration,
        project: [],
        p: 1,
      })
      this._getList()
    } else {
      this.setData({
        zxid: null,
        acDecoration: '装修',
        project: [],
        p: 1,
      })
      this._getList()
    }
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    let that = this

    let scrollH = that.data.swiperH

    if (e.scrollTop <= scrollH) {
      this.setData({
        scrollRate: e.scrollTop / scrollH,
      })
    } else {
      this.setData({
        scrollRate: 1,
      })
    }
  },
  // 跳转到搜索页
  toSearch: function (e) {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 跳转到详情页
  toDetail(e) {
    console.log()
    let id = JSON.stringify(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },
  onReachBottom: function () {
    // 页面触底时执行
    this.setData({
      p: ++this.data.p,
    })
    this._getList()
  },
})
